package com.hk.service.impl;

import com.hk.dao.IProductDao;
import com.hk.dao.impl.ProductDaoImpl;
import com.hk.model.News;
import com.hk.model.Product;
import com.hk.model.ProductMenu;
import com.hk.service.IProductMenuService;
import com.hk.service.IProductService;
import com.hk.utils.JdbcMysql;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/19 9:42
 * State：产品展示业务逻辑层
 */
public class ProductServiceImpl implements IProductService {

    IProductDao iProductDao = new ProductDaoImpl();

    IProductMenuService iProductMenuService = new ProductMenuServiceImpl();

    @Override
    public List<Product> findAll(Integer page, Integer limit) {
        ResultSet resultSet = iProductDao.findAll(page, limit);
        List<Product> productList = transverter(resultSet);
//        查询所有产品类型,匹配对应产品类型及父类型
        ProductMenu productMenu1 = new ProductMenu();
        productMenu1.setName("");
        productMenu1.setGmtCreate("");
        List<ProductMenu> productMenuList = iProductMenuService.findAll(-1, -1);
//        遍历产品
        for (Product product : productList) {
//            遍历类型
            for (ProductMenu productMenu : productMenuList) {
//                匹配子类型
                if (product.gettMenuId() == productMenu.getId()) {
                    product.settMenuName(productMenu.getName());
                    getMatching1(product,productMenuList);
                    break;
                }
                if (productMenu.getProductMenus().size() > 0) {
                    getMatching(productMenu, product);
//                    当类型匹配上后结束本次循环
                    if (product.gettMenuName() != null) {
                        getMatching1(product,productMenuList);
                        break;
                    }
                }
            }
            if (product.gettMenuName() == null) {
                product.settMenuName("暂无类型");
            }
            if (product.getMenuName() == null) {
                product.setMenuName("暂无父类型");
            }
        }
        //        释放资源
        JdbcMysql.jdbcClose();
        return productList;
    }

    @Override
    public List<Product> findAll(int page, int limit, int tMenuId) {
        ResultSet resultSet = iProductDao.findAll(page, limit,tMenuId);
        List<Product> productList = transverter(resultSet);
//        查询所有产品类型,匹配对应产品类型及父类型
        List<ProductMenu> productMenuList = iProductMenuService.findAll(-1, -1);
//        遍历产品
        for (Product product : productList) {
//            遍历类型
            for (ProductMenu productMenu : productMenuList) {
//                匹配子类型
                if (product.gettMenuId() == productMenu.getId()) {
                    product.settMenuName(productMenu.getName());
                    getMatching1(product,productMenuList);
                    break;
                }
                if (productMenu.getProductMenus().size() > 0) {
                    getMatching(productMenu, product);
//                    当类型匹配上后结束本次循环
                    if (product.gettMenuName() != null) {
                        getMatching1(product,productMenuList);
                        break;
                    }
                }
            }
            if (product.gettMenuName() == null) {
                product.settMenuName("暂无类型");
            }
            if (product.getMenuName() == null) {
                product.setMenuName("暂无父类型");
            }
        }
        //        释放资源
        JdbcMysql.jdbcClose();
        return productList;
    }

    /**
     * 递归判断产品属于那个类型
     *
     * @param productMenu
     * @param product     存储匹配的节点名称
     */
    public void getMatching(ProductMenu productMenu, Product product) {
//        判断是否具有子节点
        if (productMenu.getProductMenus().size() > 0) {
            for (ProductMenu type : productMenu.getProductMenus()) {
//                匹配类型
                if (product.gettMenuId() == type.getId()) {
                    product.settMenuName(type.getName());
//                    递归查询父节点类型
                    getMatching1(product,productMenu.getProductMenus());
                    break;
                }
//                未匹配到继续递归搜索
                getMatching(type, product);
            }
        }
    }

    /**
     * 递归判断产品属于那个父类型
     *
     * @param productMenuList 所有节点
     * @param product         存储匹配的节点名称
     */
    public void getMatching1(Product product, List<ProductMenu> productMenuList) {
//        判断是否具有子节点
        if (productMenuList.size() > 0) {
            for (ProductMenu type : productMenuList) {
//                匹配父类型
                if (product.getMenuId() == type.getId()) {
                    product.setMenuName(type.getName());
                    break;
                }
                if (type.getProductMenus().size() > 0) {
                    //                未匹配到继续递归搜索
                    getMatching1(product,type.getProductMenus());
                }
            }
        }
    }

    @Override
    public List<Product> findAllImg(int page, int limit) {
        ResultSet resultSet = iProductDao.findAll(page, limit);
        List<Product> productList = transverter(resultSet);
//        释放资源
        JdbcMysql.jdbcClose();
        return productList;
    }

    @Override
    public int count(Integer page, Integer limit) {
        ResultSet resultSet = iProductDao.count(page, limit);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            e.getMessage();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return count;
    }

    @Override
    public int count(int page, int limit, int tMenuId) {
        ResultSet resultSet = iProductDao.count(page, limit,tMenuId);
        int count = 0;
        try {
            while (resultSet.next()) {
                count = resultSet.getInt(1);
            }
            return count;
        } catch (SQLException e) {
            e.getMessage();
        }
        //        关闭资源
        JdbcMysql.jdbcClose();
        return count;
    }

    @Override
    public Product findById(Integer id) {
        ResultSet resultSet = iProductDao.findById(id);
        List<Product> productList = transverter(resultSet);
//        释放资源
        JdbcMysql.jdbcClose();
        return productList.get(0);
    }

    @Override
    public List<Product> findById(int id) {
        ResultSet resultSet = iProductDao.findById(id);
        List<Product> productList = transverter(resultSet);
//        释放资源
        JdbcMysql.jdbcClose();
        return productList;
    }

    @Override
    public Product findByImgUrl(String imgUrl) {
        ResultSet re = iProductDao.findByImgUrl(imgUrl);
        List<Product> productList = transverter(re);
//        释放资源
        JdbcMysql.jdbcClose();
        return productList.get(0);
    }

    @Override
    public int save(Product t) {
        return 0;
    }

    @Override
    public int update(Product t) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    /**
     * 将传递的数据进行转换成集合
     *
     * @param resultSet
     * @return
     */
    public List<Product> transverter(ResultSet resultSet) {
        List<Product> productList = new ArrayList<>();
        try {
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String img = resultSet.getString("img");
                String name = resultSet.getString("name");
                String detail = resultSet.getString("detail");
                BigDecimal marketPrice = resultSet.getBigDecimal("market_price");
                BigDecimal platformPrice = resultSet.getBigDecimal("platform_price");
                int tmenuId = resultSet.getInt("tmenu_id");
                int menuId = resultSet.getInt("menu_id");
                String gmtCreate = resultSet.getString("gmt_create");
                String gmtModified = resultSet.getString("gmt_modified");
//                封装对象
                Product product = new Product(id, img, name, detail, marketPrice, platformPrice, tmenuId, menuId, gmtCreate, gmtModified);
                productList.add(product);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return productList;

    }

}
